package com.shawn.integeration.rabbitmq.config;

import com.shawn.integeration.rabbitmq.constants.RabbitConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author zhangjian
 * @title: RabbitQueueConfig
 * @projectName 12-springboot学习
 * @description: MQ配置
 * @date 2021/4/2913:02
 */
@Configuration
public class RabbitQueueConfig {
    // 订单队列
    @Bean
    public Queue orderQueue() {
        //创建队列构造器并指定队列名称
        QueueBuilder queueBuilder = QueueBuilder.durable(RabbitConstants.ORDER_QUEUE);
        //如果队列持久化,这边不用设置队列过期时间
        //queueBuilder.ttl(orderQueueTTL)
        //设置死信队列的RouteKey
        queueBuilder.deadLetterRoutingKey(RabbitConstants.DEAD_ROUTE_KEY);
        //设置死信队列的Exchange
        queueBuilder.deadLetterExchange(RabbitConstants.DEAD_EXCHANGE);
        //创建队列
        return queueBuilder.build();
    }
    // topic类型交换机
    @Bean
    public TopicExchange orderTopicExchange() {
        //
        return new TopicExchange(RabbitConstants.ORDER_EXCHANGE,true,false);
    }
    // 订单队列和交换机绑定
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue())
                .to(orderTopicExchange())
                .with(RabbitConstants.ORDER_ROUTE_KEY);
    }
    // 死信队列
    @Bean
    public Queue deadQueue() {
        //
        return new Queue(RabbitConstants.DEAD_QUEUE, true);
    }
    // 死信队列采用的交换机
    @Bean
    public TopicExchange deadExchange() {
        //
        return new TopicExchange(RabbitConstants.DEAD_EXCHANGE);
    }
    // 死信队里和交换机进行绑定
    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue())
                .to(deadExchange())
                .with(RabbitConstants.DEAD_ROUTE_KEY);

    }
}
